﻿/****---------------------------------------------------------------------------------------------------->>>>
ToolTip for Flash 8 v1.0
Coded to work with ActionScript 2.0
-------------------------------------------------- 
	Created by 
		Lance Den (vistaden@msn.com , yanjun@vika.cn)
		SIAN Digital Technology co.,ltd. (www.vika.cn)

	Based on code by
		maybe is kcly (www.luar.com.hk)

	Version History:
		2007-06-14 - Modify some features.
		2007-04-07 - Modify some features,and make the usage of the class
					 to an easy way.
		2006-10-11 - First get this file from kcly

	Authors notes: 
		Class Initialization:	
			- Create an instance of the ToolTip class
			
			Example:
				import Classes.Utils.ToolTip;
				import mx.data.encoders.Num;
				var tooltip:ToolTip = new ToolTip(this);
			Note:
				ToolTip(tTarget:MovieClip,tWidth:Number,tText:String, 
				tColor:Number, txtColor:Number)
				the first and the second parameter(tTarget=this,or _root)
				can't be undefined.
			
		Draw a tip on Stage:
			
			Example:
				some_btn.onRollOver = function(){
					tooltip.text = "some text here...";
				}
	
			 
		Remove a tip on Stage:
			
			Example:
				some_btn.onRollOut = function(){
					tooltip.text = undefined;
				}

		- Lance Den 2007-04-06
<<<<----------------------------------------------------------------------------------------------------****/
//class ToolTip
class mx.utils.ToolTip
{
	// CLASS PROPERTIES
	private var $target:MovieClip;
	private var $borderC:Number;
	private var $toolTipW:Number;
	private var $toolTipH:Number;
	private var $tColor:Number;
	private var $txtColor:Number;
	private var $tipText:String;
	private var $tTxtSize:Number;
	private var $tTxtFont:String;
	private var $tTxtAlign:String;
	private var $tTxtLeading:Number;
	private var $timer:Object;
	private var $dTime:Number;
	private var $sTime:Number;
	
	private var num_stagew:Number;
	private var num_stageh:Number;
	
	public  var AI:Boolean;
	
	// CONSTRUCTOR ------------------------------------------------------------------------------->
	function ToolTip(tTarget:MovieClip,tWidth:Number,tText:String, tColor:Number, txtColor:Number) {
		//initTarget can not be undefined
		//this.$target = (tTarget==undefined) ? _root : tTarget;
		this.setRoot(tTarget);
		this.toolTipWidth = (tWidth==undefined) ? 100 : tWidth;
		//if(this.toolTipWidth>(Stage.width/2)) this.toolTipWidth=(Stage.width/2-10);
		this.text = tText;
		this.tipBackgroundColor = tColor;
		this.tipTextColor = txtColor;
		// non parametrised
		this.tipBorderColor = 0x000000;
		this.tipTextSize = 12;
		this.tipTextFont = "_sans";
		this.tipTextAlign = "left";
		this.tipTextLeading = 0;
		// unit of the two parameters below is seconds, NOT milliseconds
		this.tipDelayTime = 0.5;
		this.tipShowTime = 3;
		
		this.AI = true;
		this.num_stagew = Stage.width;
		this.num_stageh = Stage.height;
	}
	
	public function setRoot(tTarget:MovieClip):Void
	{
		this.$target = (tTarget==undefined) ? _root : tTarget;
	}
	
	public function setStageSize(num_w:Number, num_h:Number):Void
	{
		if (num_w != undefined) this.num_stagew = num_w;
		if (num_h != undefined) this.num_stageh = num_h;
	}
	
	// Private METHODS --------------------------------------------------------------------------->
	// function that after a timeout of .5 seconds, draws the tooltip by calling doDraw();
	private function drawTip() {
		this.$timer = new Object();
		$timer.displayTipDelay = setInterval(this, "doDraw", tipDelayTime);
	}
	// function to remove the tip
	private function removeTip() {
		if ($timer.tipDisplayTime) {
			clearTimer($timer.tipDisplayTime);
		}
		if ($timer.displayTipDelay) {
			clearTimer($timer.displayTipDelay);
		}
		if ($target.tooltipHolder != undefined) {
			$target.tooltipHolder.removeMovieClip();
		}
		delete this.$timer;
	}
	
	// parametrised function to clear any interval
	private function clearTimer(whatTimer)
	{
		clearInterval(whatTimer);
	}
	// modify position of tip
	private function moveTipPosition():Void
	{
		var num_x:Number = 0;
		if (this.AI==true && $target._xmouse <= (this.num_stagew / 2))
		{
			if ($target.tooltipHolder._width >= this.num_stagew / 2)
			{
				num_x = this.num_stagew - $target.tooltipHolder._width;
			}else
			{
				num_x = $target._xmouse + 10;
			}
		}else
		{
			num_x = $target._xmouse - $target.tooltipHolder._width - 6;
			if (num_x < 0) num_x = 0;
		}
		$target.tooltipHolder._x = num_x;
		
		// the tooltipHolder maybe not initialized itself completely
		// so use a var tmp to quote it for once,anyway,it's a little puzzle
		var tmp:Number = $target.tooltipHolder._height;
		
		var num_y:Number = 0;
		if (this.AI==true && $target._ymouse <= (this.num_stageh / 2))
		{
			if ($target.tooltipHolder._height >= this.num_stageh / 2)
			{
				num_y = this.num_stageh - $target.tooltipHolder._height;
			}else
			{
				num_y = $target._ymouse + 10;
			}
		}else
		{
			num_y = $target._ymouse - $target.tooltipHolder._height - 6;
		}
		$target.tooltipHolder._y = num_y;
		
		delete tmp;
	}
	// drawing the tooltip
	private function doDraw()
	{
		// clear the timer that delayed the tooltip display
		clearTimer($timer.displayTipDelay);
		// start the timer that determines the time that the tooltip will be displayed 
		$timer.tipDisplayTime = setInterval(this, "removeTip", tipShowTime);
		// tooltip holder
		$target.createEmptyMovieClip("tooltipHolder", 10000);
		// new textformat
		var $tipFormat = new TextFormat();
		$tipFormat.color   = tipTextColor;
		$tipFormat.font    = tipTextFont;
		$tipFormat.size    = tipTextSize;
		$tipFormat.leading = tipTextLeading;
		$tipFormat.align   = tipTextAlign;
		// create textfield inside tooltipHolder
		$target.tooltipHolder.createTextField("tipField", 5, 0, 0, 0, 0);
		$target.tooltipHolder["tipField"]._visible = false;
		$target.tooltipHolder["tipField"].border = true;
		$target.tooltipHolder["tipField"].borderColor = this.tipBorderColor;
		$target.tooltipHolder["tipField"].background = true;
		$target.tooltipHolder["tipField"].backgroundColor = this.tipBackgroundColor;
		$target.tooltipHolder["tipField"].selectable = false;
		$target.tooltipHolder["tipField"].embedFonts = false;
		$target.tooltipHolder["tipField"]._width = this.toolTipWidth;
		// [20070704]wordWrap t/f ?
		$target.tooltipHolder["tipField"].wordWrap = false;
		$target.tooltipHolder["tipField"].autoSize = true;
		$target.tooltipHolder["tipField"].type = "dynamic";
		$target.tooltipHolder["tipField"].text = $tipText;
		$target.tooltipHolder["tipField"].setTextFormat($tipFormat);
		
		if($target.tooltipHolder["tipField"]._width>this.toolTipWidth)
		{
			$target.tooltipHolder["tipField"]._width = this.toolTipWidth;
			$target.tooltipHolder["tipField"].wordWrap = true;
			$target.tooltipHolder["tipField"].autoSize = true;
			
		}
		$target.tooltipHolder["tipField"]._visible = true;
		// move tip to a new position
		this.moveTipPosition();
	}
	
	// SETTERS ----------------------------------------------------------------------------------->
	public function set tipBackgroundColor(tColor:Number):Void {
		if (tColor == undefined) {
			this.$tColor = 0xFFFFE1;
		} else {
			this.$tColor = tColor;
		}
	}
	public function set tipTextColor(txtColor:Number):Void {
		if (txtColor == undefined) {
			this.$txtColor = 0x00000;
		} else {
			this.$txtColor = txtColor;
		}
	}
	// modified by Lance Den
	public function set text(newText:String):Void {
		this.$tipText = "";
		this.removeTip();
		if(newText!=undefined){
			this.$tipText = newText;
			this.drawTip();
		}else{
			this.$tipText = "";
			this.removeTip();
		}
	}
	public function set toolTipHeight(toolTipH:Number):Void {
		this.$toolTipH = toolTipH;
	}
	public function set toolTipWidth(toolTipW:Number):Void {
		this.$toolTipW = toolTipW;
	}
	public function set tipBorderColor(borderC:Number):Void {
		this.$borderC = borderC;
	}
	public function set tipTextSize(tTxtSize:Number):Void {
		this.$tTxtSize = tTxtSize;
	}
	public function set tipTextFont(tTxtFont:String):Void {
		this.$tTxtFont = tTxtFont;
	}
	public function set tipTextAlign(tTxtAlign:String):Void {
		this.$tTxtAlign = tTxtAlign;
	}
	public function set tipTextLeading(tTxtLeading:Number):Void {
		this.$tTxtLeading = tTxtLeading;
	}
	public function set tipDelayTime(dTime:Number):Void {
		this.$dTime = dTime*1000;
	}
	public function set tipShowTime(sTime:Number):Void {
		this.$sTime = sTime*1000;
	}
	
	// GETTERS ----------------------------------------------------------------------------------->
	public function get tipBackgroundColor():Number {
		return $tColor;
	}
	public function get tipTextColor():Number {
		return $txtColor;
	}
	public function get toolTipHeight():Number {
		return $toolTipH;
	}
	public function get toolTipWidth():Number {
		return $toolTipW;
	}
	public function get tipBorderColor():Number {
		return $borderC;
	}
	public function get tipTextSize():Number {
		return $tTxtSize;
	}
	public function get tipTextFont():String {
		return $tTxtFont;
	}
	public function get tipTextAlign():String {
		return $tTxtAlign;
	}
	public function get tipTextLeading():Number {
		return $tTxtLeading;
	}
	public function get tipDelayTime():Number {
		return $dTime;
	}
	public function get tipShowTime():Number {
		return $sTime;
	}
}
